export function comp(id,idx=0) {
	if (id) {
		return httpVueLoader('../component/' + id + '.vue?v=' + Math.random());   //idx为索引序号，方便知道
		//return httpVueLoader('component/' + id + '.vue')   //idx为索引序号，方便知道
	} else {
		return false
	}
}
var Config = {
    variable :  {
        url: 'https://houduan.leantong.com/api/',
        menu: [
            {title:'首页', icon:'icon ion-home'},
            {title:'商品', icon:'icon ion-android-apps'},
            {title:'发现', icon:'icon ion-cube'},
            {title:'购物车', icon:'icon ion-ios-cart'},
            {title:'我的', icon:'icon ion-person'}
        ],
        color: "#ee6c21" //主题色
    }
};
var route = {
		//第2个参数仅是索引作用
		'index': comp('index', 0),
		'job': comp('job/index', 1),
		'msg': comp('msg/index', 2),
		'user': comp('user/index', 3),
		'bbs': comp('bbs/index', 4),
		'tontru': comp('tontru/index', 5),
		'pay': comp('pay/index', 6),
		'job-panel': comp('job/panel', 7),
		'job-employer-publish': comp('job/employer/publish', 8),
		'job-employer-list': comp('job/employer/list', 9),
		'job-employee-list': comp('job/employee/list', 10),
		'job-employee-settle': comp('job/employee/settle/index', 11),
		'job-employee-settle-detail': comp('job/employee/settle/detail', 12),
		'job-employee-clockin': comp('job/employee/clockin/index', 13),
		'job-appraise': comp('job/appraise', 14),
		'job-detail': comp('job/detail', 15),
		'pay-list': comp('pay/list', 16),
		'pay-deposit': comp('pay/deposit', 17),
		'pay-withdraw': comp('pay/withdraw', 18),
		'pay-transfer': comp('pay/transfer', 19),
		'pay-payment': comp('pay/payment', 20),
		'user-appeal': comp('user/appeal/index', 21),
		'user-appeal-detail': comp('user/appeal/detail', 22),
		'user-appeal-appeal': comp('user/appeal/appeal', 23),
		'user-appraise': comp('user/appraise/index', 24),
		'user-appraise-detail': comp('user/appraise/detail', 25),
		'user-appraise-appraise': comp('user/appraise/appraise', 26),
		'user-profile-realname': comp('user/profile/realname', 27, '实名认证'),
		'user-profile-card': comp('user/profile/card', 28, '工作名片'), 
		'user-auth': comp('user/auth', 29),
		'tontru-intelligencer-publish': comp('tontru/intelligencer/publish', 30),
		'tontru-intro': comp('tontru/intro', 31),
		'user-setting': comp('user/setting/index', 32),
		'user-setting-password': comp('user/setting/password', 33),
		'pay-wallet': comp('pay/wallet', 34),
		'job-worker': comp('job/worker', 35),
		'user-setting-mobile': comp('user/setting/mobile', 36),
		'user-suggest': comp('user/suggest/index', 37),
		'user-suggest-list': comp('user/suggest/list', 38),
		'job-employer-pay': comp('job/employer/pay', 39),
		'user-profile-experience': comp('user/profile/experience', 40, '项目经验'),
		'user-profile-certificate': comp('user/profile/certificate', 41, '证书认证'),
		'user-collect': comp('user/collect/index', 42),
		'user-insurance': comp('user/insurance/index', 43),
		'job-employer-apply': comp('job/employer/apply', 44),
		'job-employer-wage': comp('job/employer/wage', 45),
		'job-employer-settle': comp('job/employer/settle/index', 46),
		'job-employer-settle-detail': comp('job/employer/settle/detail', 47, '核算工资'),
		'job-progress': comp('job/progress/index', 48),
		'job-progress-add': comp('job/progress/add', 49),
		'job-progress-detail': comp('job/progress/detail', 50),
		'user-profile-edit': comp('user/profile/edit', 51, '完善基础信息'),
		'job-employee-clockin-clockin': comp('job/employee/clockin/clockin', 52, '打卡签到'),
		'tontru-list': comp('tontru/list', 53, '螺旋同创参与的项目列表（含情报、销售、施工）'),
		'tontru-detail2': comp('tontru/detail', 54, '螺旋同创订单详情'),
		'tontru-partner-apply': comp('tontru/partner/apply', 55, '申请螺旋同创施工合伙人'),
		'tontru-detail': comp('tontru/detail', 56, '螺旋同创进度'),
		'tontru-partener-list': comp('tontru/partener/list', 57, '螺旋同创施工合伙人接单记录'),
		'tontru-salesman-initiate': comp('tontru/salesman/initiate', 58, '螺旋同创签合同立项'),
		'tontru-salesman-follow': comp('tontru/salesman/follow', 59, '螺旋同创施工合伙人接单记录'),
		'tontru-salesman-list': comp('tontru/salesman/list', 60, '螺旋同创施工合伙人接单记录'),
		'tontru-salesman-assign': comp('tontru/salesman/assign', 61, '螺旋同创分配施工合伙人'),
		'credit': comp('credit/index', 62, '物联征信'),
		'credit-search': comp('credit/search', 63, '物联征信查询'),
		'credit-report': comp('credit/report', 64, '物联征信举报'),
		'admin': comp('admin', 65, '后台首页'),
		'admin-user': comp('admin/user/index', 66, '后台用户管理'),
		'admin-tontru-apply': comp('admin/tontru/apply', 67, '螺旋同创施工合伙人审核'),
		'common-menu': comp('common/menu'),
		'common-sys': comp('common/sys'),
	};
var  Utils = {
    variable :  {
        url: 'https://hanjiang.wei3ma.com/api/',
        menu: [
            {title:'首页', icon:'icon ion-home'},
            {title:'商品', icon:'icon ion-android-apps'},
            {title:'发现', icon:'icon ion-cube'},
            {title:'购物车', icon:'icon ion-ios-cart'},
            {title:'我的', icon:'icon ion-person'}
        ],
        color: "#ee6c21" //主题色
    },
    http(u, d = '', s = 'json', token='', t = 5000) {
        return new Promise(function(resolve, reject) {
            if (s=='json' || s==='') {
                /*
                var xhr = new XMLHttpRequest();
                d = Utils.jsonToUrl(d);
                var m = d ? 'POST' : 'GET';
                token === '' ? token = Utils.getCookie("token") : '';
                xhr.open(m, u, true);
                xhr.setRequestHeader("Authori-zation", "Bearer " + token);
                (d) && (xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"));
                xhr.onreadystatechange = () => {
                    resolve(JSON.parse(xhr.responseText));
                };
                xhr.send(d);
                */
                var m = d ? 'POST' : 'GET';
                token === '' ? token = Utils.getCookie("token") : '';
                $.ajax({
                    method: m,
                    dataType: s,
                    async: true,//使用同步的方式,true为异步方式
                    headers: {
    	              'Authori-zation' : 'Bearer ' + token
    	            },
                    url: u,
                    data: d,
                    success: function(res, xhr) {
                        resolve(res);
                    },
                });
            } else if (s=='jsonp') {
                var n = ('jsonp_' + Math.random()).replace(".", "");
                var dom = document.getElementsByTagName('head')[0];
                var ele = document.createElement('script');
                dom.appendChild(ele);
                window[n] = (json) => {
                    dom.removeChild(ele);
                    clearTimeout(ele.timer);
                    window[n] = null;
                    console.log('jsonp',json);
                    resolve(json);
                    //options.success && options.success(json);
                };
                d.callback = n;
                d = Utils.jsonToUrl(d);
                console.log(d);
                ele.src = u + '?' + d;
                ele.timer = setTimeout(function () {
                    window[n] = null;
                    dom.removeChild(ele);
                    options.fail && options.fail({ message: "超时" });
                }, t);
            }
            
 
        })
    },
	jsonToUrl (data) {
	    var params;
	    if (typeof data == 'object') {
            params = Object.keys(data).map(key => {
                return encodeURIComponent(key) + "=" + encodeURIComponent(data[key]);
            }).join("&");
	    } else {
	        params = data;
	    }
        return params;
	},
	//取回cookie
	getCookie (cname) {
		var name = cname + "=";
		var ca = document.cookie.split(';');
		var val;
		for (var i = 0; i < ca.length; i++) {
			var c = ca[i];
			while (c.charAt(0) == ' ') c = c.substring(1);
			if (c.indexOf(name) != -1) val = c.substring(name.length, c.length);
		}
		(!val || typeof val == 'undefined' || val == 'undefined' || val == 'null') ? val = '' : '';
		return val;
	},
    link(u) {
        for (var i in u){
            var d,
            	l = u[i],
            	r = Math.random();
            	
            if ((l + '?').indexOf('.js')!=-1){
                d = document.createElement('script');
                d.type = "text/javascript";
                d.src = l + '?v=' + r;
            } else if ((l + '?').indexOf('.css')!=-1){
                d = document.createElement('link');
                d.rel = "stylesheet";
                d.href = l + '?v=' + r;
            }
            document.getElementsByTagName('head')[0].appendChild(d);
        }
    },
    Url:{
        Get : function(item){
            var svalue = location.search.match(new RegExp("[\?\#\&]" + item + "=([^\&]*)(\&?)","i"));
            return svalue ? svalue[1] : svalue;
        },
		GetAll: function () {
		    var url = decodeURIComponent(location.search); //获取url中"?"符后的字串
		    console.log(url);
		    var theRequest = {};
		    if (url.indexOf("?") != -1) {
		        let str = url.substr(1);
		        strs = str.split("&");
		        for (var i = 0; i < strs.length; i++) {
		            let tempArr = strs[i].split("=");
		            // 类型转换
		            if (tempArr[1] == 'true') {
		                tempArr[1] = true;
		            }
		            if (tempArr[1] == 'false') {
		                tempArr[1] = false;
		            }
		            //if (/^[\d|.]+$/.test(tempArr[1])) {
		            //    tempArr[1] = Number(tempArr[1]);
		            //}
		            // 写入对象
		            if (tempArr[0].search(/\[.*]/) == -1) {
		                theRequest[tempArr[0]] = tempArr[1];
		            } else {
		                // 数组
		                var key = tempArr[0].replace(/\[.*]/, '');
		                if (!theRequest[key]) {
		                    theRequest[key] = [tempArr[1]];
		                } else {
		                    theRequest[key].push(tempArr[1]);
		                }
		            }
		        }
		    }
		    
		    return theRequest;
		},
        Replace(url, arr){
        	var u  = '';
			for(var key in arr){
			    if (arr[key] || arr[key]===0 || arr[key]==='0') {u += '&' + key +'='+ arr[key];}
			}
			u.length > 0 ? url = url + "?" + u : '';
			url = url.replace(/\?&/,'?');
			//history.pushState({}, "页面标题", url);
			history.replaceState({}, '', url);
        }
    },
    href(u) {
		location.replace(u);	
	}
};	
export {route, Config, Utils};
